/*
 *  testClassKey.cpp
 *  kangmodb
 *
 *  Created by 강모 김 on 11. 4. 25..
 *  Copyright 2011 강모소프트. All rights reserved.
 *
 */


#include "gtest/gtest.h"

#include "kangmodb.h"
#include "testCommon.h"

/** @brief Test stgKey class 
 */
class ClassKeyTest : public testing::Test {
protected:  // You should make the members protected s.t. they can be
	// accessed from sub-classes.
	
	// virtual void SetUp() will be called before each test is run.  You
	// should define it if you need to initialize the varaibles.
	// Otherwise, this can be skipped.
	virtual void SetUp() {
	}
	
	// virtual void TearDown() will be called after each test is run.
	// You should define it if there is cleanup work to do.  Otherwise,
	// you don't have to provide it.
	//
	virtual void TearDown() {
	}
protected:
};



TEST_F(ClassKeyTest, Constructor_1arg) {
	const char * KEY_STR = "Hyunsook";
	stgKey src(KEY_STR);
	
	EXPECT_TRUE( src.getKey() == KEY_STR );
	EXPECT_TRUE( src.getLength() == strlen(KEY_STR) );
}

TEST_F(ClassKeyTest, DISABLED_Constructor_1arg_EmptyString) {
	const char * KEY_STR = "";
	stgKey src(KEY_STR);
	
	EXPECT_TRUE( src.getKey() == KEY_STR );
	EXPECT_TRUE( src.getLength() == strlen(KEY_STR) );
}

TEST_F(ClassKeyTest, DISABLED_Constructor_Assertion) {
    EXPECT_DEATH({ stgKey key(NULL); }, "Assertion.*\n");
}

TEST_F(ClassKeyTest, CopyAssignment) {
	const char * KEY_STR = "Hyunsook";
	const stgValueLength KEY_LEN = 8;
	// Test Constructor with 2 args
	stgKey src(KEY_STR, KEY_LEN);
	// Test Constructor without any arg
	stgKey dst;
	
	// Before the assignment, dst has the same key.
	EXPECT_TRUE( src.getKey() == KEY_STR );
	EXPECT_TRUE( src.getLength() == KEY_LEN );
	
	dst = src;
	
	// After the assignment, dst has the same key.
	EXPECT_TRUE( dst.getKey() == KEY_STR );
	EXPECT_TRUE( dst.getLength() == KEY_LEN );
}


static int compare(const char * key1_str, const stgValueLength key1_len, const char * key2_str, const stgValueLength key2_len)
{
	stgKey key1(key1_str, key1_len);
	stgKey key2(key2_str, key2_len);
	
	return key1.compare(key2);
}  

TEST_F(ClassKeyTest, KeyComparison) {
	// Basic Tests
	EXPECT_TRUE( compare("a", 1, "a", 1) == 0 );
	EXPECT_TRUE( compare("ab", 2, "a", 1) > 0 );
	EXPECT_TRUE( compare("a", 1, "ab", 2) < 0 );

	// Having more characters after the given length does not affect the result of comparison
	EXPECT_TRUE( compare("a0", 1, "a0", 1) == 0 );
	EXPECT_TRUE( compare("ab0", 2, "a0", 1) > 0 );
	EXPECT_TRUE( compare("a0", 1, "ab0", 2) < 0 );

	// Having more characters after the given length does not affect the result of comparison
	EXPECT_TRUE( compare("a0", 1, "a1", 1) == 0 );
	EXPECT_TRUE( compare("ab0", 2, "a1", 1) > 0 );
	EXPECT_TRUE( compare("a0", 1, "ab1", 2) < 0 );

	// Having more characters after the given length does not affect the result of comparison
	EXPECT_TRUE( compare("a1", 1, "a0", 1) == 0 );
	EXPECT_TRUE( compare("ab1", 2, "a0", 1) > 0 );
	EXPECT_TRUE( compare("a1", 1, "ab0", 2) < 0 );
}

